#include <stdio.h>
#include <stdlib.h>
#include "linktab.h"
#define PRIME 13
LT_NODE *hash[PRIME];
void searchHash(int num);
int main()
{
    int i, k, num, choice;
    int hashvalue, status;
    int number;

    FILE *p;
    struct node *head = NULL;
    //关键字列表保存在2.txt文件中，其中第一个值为关键字的个数
    //其他值为具体的关键字，各个关键字之间用空格隔开
    p = fopen("2.txt", "r");
    if (p == NULL)
    {
        printf("cannot open file 2.txt");
        exit(0);
    }
    fscanf(p, "%d", &num); //关键字个数，大程序里不需要
    printf("%d\n", num);
    for (i = 0; i < PRIME; i++)
    {
        hash[i] = (LT_NODE *)malloc(sizeof(LT_NODE)); //分配内存
        hash[i]->key = i;
        hash[i]->next = NULL; //初始化
    }
    for (i = 0; i < num; i++)
    {
        fscanf(p, "%d", &k);
        printf("%d ", k);
        ListAppend(hash[k % 13], k); //k%13即为关键字的哈希值
    }                                //从文件中读取关键字，并建立哈希表

    printf("\n");
    printf("1.打印哈希表\n2.查找\n3.退出\n");
    do
    {
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            for (i = 0; i < PRIME; i++)
            {
                walk(hash[i]);
            }
            break;
        case 2:
            printf("输入要查找的数:");
            scanf("%d", &number);
            searchHash(number); //只需查找对应哈希值的链表
            break;
        default:;
        }
    } while (choice != 3);

    return 0;
}

void searchHash(int num) //查找
{
    LT_NODE *p = hash[num % PRIME];
    int pos = 0, status = 0;
    p = p->next;
    while (p)
    {
        pos++;
        if (p->key == num)
        {
            printf("有，在第%d行，第%d位\n", num % PRIME + 1, pos);
            status = 1;
            break;
        }
        p = p->next;
    }
    if (status == 0)
        printf("没有\n");
}

